﻿using Microsoft.Reporting.WinForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MESPlotV1
{
    public partial class F不合格通知处理单套打 : Form
    {
        public const string TBF_单据编号 = "BillCode";
        public const string TBF_单据日期 = "CreateTime";
        public const string TBF_修改日期 = "ModifyTime";
        public const string TBF_项目名称 = "ProjectName";
        public const string TBF_合同编号 = "ContractCode";
        public const string TBF_产品名称 = "ProductName";
        public const string TBF_车间班组 = "WorkTeamName";
        public const string TBF_物料名称 = "PartName";
        public const string TBF_工序名称 = "ProcessName";
        public const string TBF_不合格数量 = "RejectQuantity";
        public const string TBF_规格型号 = "Model";
        public const string TBF_物料编码 = "PartCode";
        public const string TBF_技术要求 = "TechCommand";
        public const string TBF_实测结果 = "InspectionRecord";
        public const string TBF_领导批示 = "LeaderRemark";
        public const string TBF_责任部门 = "DepartName";
        public const string TBF_检验员 = "InspectorName";


        private SQLSerConfigReg SQLSerCFG { get; }
        public F不合格通知处理单套打(SQLSerConfigReg tSQLSerCFG)
        {
            InitializeComponent();
            SQLSerCFG = tSQLSerCFG;
            string sRDLCFile;
            sRDLCFile = @"不合格通知处理单套打/不合格通知处理单20250904.rdlc";
            //sRDLCFile = @"不合格通知处理单套打/Report1.rdlc";
            if (File.Exists(sRDLCFile))
            {
                reportViewer1.LocalReport.ReportPath = sRDLCFile;
            }
        }

        private void button查询单据_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox单据编号.Text))
            {
                string sSQL = @"
                    select top 1 a.* ,B.DepartName
                     from QMS_RejectPartReport a
                     left join (select distinct TeamCode,DepartName from SYS_WorkGroup) b on B.TeamCode=A.WorkTeamCode
                     where a.BillCode='{0}'
                    ";
                sSQL = string.Format(sSQL, textBox单据编号.Text);
                int iCount = 0;
                DataTable tDatas = SQLSerHelper.runSelect(sSQL,out iCount, SQLSerCFG);
                if (iCount > 0)
                {
                    dataGridView1.DataSource = tDatas;
                    button打印单据.Enabled = true;
                }
            }
        }

        private void button打印单据_Click(object sender, EventArgs e)
        {
            DataTable tDatas = (DataTable)dataGridView1.DataSource;
            if (tDatas != null && tDatas.Rows.Count > 0 && File.Exists(reportViewer1.LocalReport.ReportPath))
            {
                DataRow tDRow = tDatas.Rows[0];
                string sDTmp = tDRow[TBF_单据日期].ToString();
                if (string.IsNullOrEmpty(sDTmp))
                    sDTmp= tDRow[TBF_修改日期].ToString();
                // 2. 准备参数（与 RDLC 中定义的参数名必须一致，区分大小写）
                ReportParameter[] parameters = new ReportParameter[]
                {
                        new ReportParameter("RP单据编号", tDRow[TBF_单据编号].ToString()), // 
                        new ReportParameter("RP单据日期",  sDTmp), // 
                        new ReportParameter("RP项目名称",  tDRow[TBF_项目名称].ToString()), // 
                        new ReportParameter("RP合同编号",  tDRow[TBF_合同编号].ToString()), // 
                        new ReportParameter("RP产品名称",  tDRow[TBF_产品名称].ToString()), // 
                        new ReportParameter("RP车间班组",  tDRow[TBF_车间班组].ToString()), // 
                        new ReportParameter("RP物料名称", tDRow[TBF_物料名称].ToString()), // 
                        new ReportParameter("RP工序名称", tDRow[TBF_工序名称].ToString()), // 
                        new ReportParameter("RP不合格数量", tDRow[TBF_不合格数量].ToString()), // 
                        new ReportParameter("RP规格型号", tDRow[TBF_规格型号].ToString()), // 
                        new ReportParameter("RP物料编码", tDRow[TBF_物料编码].ToString()), // 
                        new ReportParameter("RP技术要求",  tDRow[TBF_技术要求].ToString()), // 
                        new ReportParameter("RP实测结果",  tDRow[TBF_实测结果].ToString()),
                        new ReportParameter("RP领导批示", tDRow[TBF_领导批示].ToString()),
                        new ReportParameter("RP责任部门",  tDRow[TBF_责任部门].ToString()),
                        new ReportParameter("RP检验员",  tDRow[TBF_检验员].ToString())
                };
                // 传递参数到 RDLC 模板
                reportViewer1.LocalReport.SetParameters(parameters);

                reportViewer1.RefreshReport();

            }
        }

        private void reportViewer1_RenderingComplete(object sender, RenderingCompleteEventArgs e)
        {
            if (reportViewer1.DisplayMode!= DisplayMode.PrintLayout)
                reportViewer1.SetDisplayMode(DisplayMode.PrintLayout);
        }
    }
}
